package com.yitao.cms.service.product;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.bean.PagedInput;
import com.skipper.base.bean.PagedOutput;
import com.skipper.base.data.querydsl.PredicateBuilder;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.product.BuiltPlaceIconDto;
import com.yitao.client.model.product.BuiltPlaceIconModel;
import com.yitao.client.model.product.QBuiltPlaceIconModel;
import com.yitao.cms.constant.UserSession;
import com.yitao.cms.model.operationUser.OperationUserCmsModel;
import com.yitao.cms.utils.BaseUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: wwl
 * @Date: 2019/6/26 16:28
 * @Description:
 */
@Service
public class BuiltPlaceIconService extends GenericEntityService<BuiltPlaceIconModel, BuiltPlaceIconDto, QBuiltPlaceIconModel> {
    public BuiltPlaceIconService(EntityManager entityManager) {
        super(entityManager);
    }

    BaseUtils base = BaseUtils.getInstance();

    /**
     * 产地-图标编辑接口
     *
     * @param request
     * @param id
     * @param name
     * @param enName
     * @param icon
     * @return
     */
    @Transactional
    public DataOutput<BuiltPlaceIconDto> saveBuiltPlaceIcon(HttpServletRequest request, Long id, String name, String enName, String icon) {
        if (StringUtils.isBlank(name) || StringUtils.isBlank(icon)) {
            return base.getBadErrorMsg("产地名不能为空!");
        }
        OperationUserCmsModel operationUserCmsModel = UserSession.getUserSession(request);
        if (operationUserCmsModel == null) {
            return base.getBadErrorMsg("用户未登录!");
        }
        BuiltPlaceIconModel builtPlaceIconModel = new BuiltPlaceIconModel();
        if (id != null && id > 0L) {//编辑
            BuiltPlaceIconModel builtPlaceIconModel1 = queryFactory.select(Q).from(Q).where(Q.id.eq(id)).fetchFirst();
            if (builtPlaceIconModel1 == null) {
                return base.getBadErrorMsg("未查到相关数据，请确认输入是否有误!");
            } else {
                BeanUtils.copyProperties(builtPlaceIconModel1, builtPlaceIconModel);
                builtPlaceIconModel.setName(name);
                builtPlaceIconModel.setEnName(enName);
                builtPlaceIconModel.setIcon(icon);
                builtPlaceIconModel.setLastUpdateBy(operationUserCmsModel.getCreateBy());
                builtPlaceIconModel.setLastUpdateTime(base.getNow());
            }
        } else {//新增
            BuiltPlaceIconModel builtPlaceIconModel1 = queryFactory.select(Q).from(Q).where(Q.name.eq(name).and(Q.isDelete.eq(false))).fetchFirst();
            if (builtPlaceIconModel1!=null){
                return base.getBadErrorMsg("该产地名已存在!");
            }
            builtPlaceIconModel.setName(name);
            builtPlaceIconModel.setEnName(enName);
            builtPlaceIconModel.setIcon(icon);
            builtPlaceIconModel.setDelete(false);
            builtPlaceIconModel.setCreateBy(operationUserCmsModel.getId());
            builtPlaceIconModel.setCreateTime(base.getNow());
            builtPlaceIconModel.setLastUpdateBy(operationUserCmsModel.getCreateBy());
            builtPlaceIconModel.setLastUpdateTime(base.getNow());
        }
        BuiltPlaceIconModel builtPlaceIconModel1 = repository.save(builtPlaceIconModel);
        return base.getSuccessMsg(convertEntityToDto(builtPlaceIconModel1));
    }

    /**
     * 查询列表
     *
     * @param id
     * @param name
     * @param enName
     * @return
     */
    public PagedOutput<BuiltPlaceIconDto> builtPlaceIconList(Long id, String name, String enName, Integer pageNo, Integer pageSize) {
        PagedInput pagedInput = new PagedInput(pageSize, pageNo);
        PagedOutput<BuiltPlaceIconModel> pagedOutput = repository.pagingList(PredicateBuilder.conjunction()
                .ifNotNullThenAnd(id, () -> Q.id.eq(id))
                .ifHasTextThenAnd(name, () -> Q.name.eq(name))
                .ifHasTextThenAnd(enName, () -> Q.enName.eq(enName))
                .and(()->Q.isDelete.eq(false))
                .get(), pagedInput, new OrderSpecifier<>(Order.DESC, Q.lastUpdateTime));
        List<BuiltPlaceIconDto> list = pagedOutput.getData().stream().map(this::convertEntityToDto).collect(Collectors.toList());
        return new PagedOutput(pagedInput, pagedOutput.getTotalElements(), list);
    }

    /**
     * 查询列表
     *
     * @param name
     * @param enName
     * @return
     */
    public DataOutput<BuiltPlaceIconDto> builtPlaceIconListAll( String name, String enName) {
       List<BuiltPlaceIconModel>  builtPlaceIconModelList= repository.findAll(PredicateBuilder.conjunction()
                .ifHasTextThenAnd(name, () -> Q.name.eq(name))
                .ifHasTextThenAnd(enName, () -> Q.enName.eq(enName))
                .and(() -> Q.isDelete.eq(false))
                .get());
        List<BuiltPlaceIconDto> list = builtPlaceIconModelList.stream().map(this::convertEntityToDto).collect(Collectors.toList());
        return new DataOutput(list);
    }
    /**
     * 查询列表
     *
     * @param id
     * @return
     */
    public DataOutput<BuiltPlaceIconDto> builtPlaceIconById(Long id) {
        if (id == null || id == 0L) {
            return base.getBadErrorMsg("id不能为空!");
        } else {
            BuiltPlaceIconModel builtPlaceIconModel = queryFactory.select(Q).from(Q).where(Q.id.eq(id)).fetchFirst();
            if (builtPlaceIconModel != null) {
                return new DataOutput<>(convertEntityToDto(builtPlaceIconModel));
            } else {
                return base.getBadErrorMsg("未查到数据信息!");
            }

        }
    }
    /**
     * 查询列表
     *
     * @param id
     * @return
     */
    @Transactional
    public DataOutput<BuiltPlaceIconDto> updateBuiltPlaceIconStatus(HttpServletRequest request,Long id ) {
        OperationUserCmsModel operationUserCmsModel = UserSession.getUserSession(request);
        if (operationUserCmsModel == null ) {
            return base.getBadErrorMsg("用户未登录!");
        }
        if (id == null || id == 0L) {
            return base.getBadErrorMsg("id输入有误!");
        } else {
            BuiltPlaceIconModel builtPlaceIconModel = queryFactory.select(Q).from(Q).where(Q.id.eq(id)).fetchFirst();
            if (builtPlaceIconModel != null) {
              Integer result =  repository.updateByWhere(Q.id.eq(id),it->it.set(Q.isDelete,true).set(Q.lastUpdateBy, operationUserCmsModel.getId()).set(Q.lastUpdateTime,base.getNow()));
              if (result>0){
                  return base.getSuccessMsg();
              }else{
                  return base.getErrorMsg("修改失败，请重新操作!");
              }
            } else {
                return base.getBadErrorMsg("未查到数据信息!");
            }

        }
    }

}
